package corrida.stack;

public class Stack {
	
	private StackNode top;
	private StackNode base;
	private int quantity;
	
	public Stack() {
		top = null;
		base = null;
		quantity = 0;
	}
	
	public boolean isEmpty()
	{
		if ( this.quantity == 0 )
			return true;
		else
			return false;
	}
	
	public int getQuantity() {
		return this.quantity;
	}
	
	public void push (String elemento){
		StackNode newNode = new StackNode(elemento.toLowerCase());
		
		if (isEmpty()) {
			this.base = newNode;
		}
		else {
			newNode.setNext(this.top);
		}
		
		this.top = newNode;
		this.quantity++;
	}
	
	public String pop () {
		StackNode aux = null;
		String value = null;
		
		if (isEmpty()) {
			return null;
		}
		
		aux = this.top;
		value = aux.getInfo();
		this.top = this.top.getNext();
		aux = null;
		this.quantity--;
		
		if (isEmpty()) {
			this.base = null;
		}
		
		return value;
	}
	
	public void listElements() {
		int i = 1;
		StackNode sn = this.top;
		
		while (sn != null) {
			System.out.println("Elem " + i + " " + sn.getInfo());
			sn = sn.getNext();
			i++;
		}			
	}
}